#include "config.h"

#include <cstdlib>
#include <sstream>
#include <stdexcept>

namespace crawler { namespace server {
Config::Config(int argc, char * const *argv) :
  server_port_(48600),
  store_db_path_("storedb"),
  homepage_("http://www.education.com/"),
  need_help_(false) {
  std::ostringstream err;
  int opt;
  while ((opt = getopt(argc, argv, "s:p:H:h")) != -1) {
    switch (opt) {
     case 's':
      store_db_path_ = optarg;
      break;
     case 'p':
      server_port_ = atoi(optarg);
      break;
     case 'H':
      homepage_ = optarg;
      if (homepage_.find("http://")!=0 || *homepage_.rbegin()!='/') {
        throw std::runtime_error("homepage should look like http://something/");
      }
      break;
     case 'h':
      need_help_ = true;
      break;
     case ':':
      err << "option:" << (char)optopt <<  " needs a value";
      throw std::runtime_error(err.str());
      break;
     case '?':
      err << "unknow option:" << (char)optopt;
      throw std::runtime_error(err.str());
      break;
    }
  }
}
}}

